﻿#include "sendemailtask.h"
#include "computermanager.h"
#include "syscfg.h"

#define FIRST_START_SLEEP_SECONDS 20

#define NO_ALARM_CKECK_INTERVAL_SECONDS 10

SendEmailTask gSendEmailTask;

SendEmailTask::SendEmailTask()
{

}

int SendEmailTask::start()
{
    //为了防止第一次启动的时候，告警数据还不够稳定，这里先sleep20秒
    int sleep1 = FIRST_START_SLEEP_SECONDS * 2;
    for (int i = 0; i < sleep1; i ++)
    {
        if(state == RunableStateStarted)
        {
            Sleep(500);
        }
        else
        {
            return f_success;
        }
    }

    while (state == RunableStateStarted)
    {
        bool sendAlarm =  gComputerManager.checkAndSendAlarm();
        if(sendAlarm)
        {
            int sleep2 = SEND_EMAIL_INTERVAL_SECONDS * 2;
            //已经触发了Alarm,sleep(SEND_EMAIL_INTERVAL_SECONDS)秒再检查
            for (int i = 0; i < sleep2; i ++)
            {
                if(state == RunableStateStarted)
                {
                    Sleep(500);
                }
                else
                {
                    return f_success;
                }
            }
        }
        else
        {

            //没有触发告警，这过10秒在检查
            int sleep3 = NO_ALARM_CKECK_INTERVAL_SECONDS * 2;
            for (int i = 0; i < sleep3; i ++)
            {
                if(state == RunableStateStarted)
                {
                    Sleep(500);
                }
                else
                {
                    return f_success;
                }
            }
        }
    }
    return f_success;
}
